/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core.action.delete;

import com.inspur.edp.bff.api.manager.context.DeleteContext;
import com.inspur.edp.bff.spi.action.delete.AbstractDeleteAction;
import com.inspur.edp.bff.spi.action.delete.AfterDeleteAction;
import com.inspur.edp.bff.spi.action.delete.BeforeDeleteAction;
import com.inspur.edp.bff.spi.action.delete.DefaultDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.AbstractMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.AfterMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.BeforeMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.DefaultMultiDeleteAction;
import com.inspur.edp.bff.spi.assembler.AbstractExtendDeleteAssembler;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.collection.VMActionCollection;
import com.inspur.edp.formserver.viewmodel.common.InternalExtendActionUtil;

import java.util.ArrayList;

import static com.inspur.edp.bff.engine.core.action.BffEngineAssemblerBase.getActions;

public class BffEngineDeleteAssembler extends AbstractExtendDeleteAssembler {

    private GspViewModel vo;

    public BffEngineDeleteAssembler(GspViewModel vo) {
        this.vo = vo;
    }

    @Override
    protected ArrayList<BeforeDeleteAction> getBeforeDeleteActions(DeleteContext deleteContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getBeforeDeleteActions();
        return getActions(BeforeDeleteAction.class, actions, getParamTypes(), getParamValues(deleteContext), null, null);
    }

    @Override
    protected ArrayList<AbstractDeleteAction> getDeleteActions(DeleteContext deleteContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getDeleteActions();
        return getActions(
                AbstractDeleteAction.class,
                actions,
                getParamTypes(),
                getParamValues(deleteContext),
                new DefaultDeleteAction(deleteContext),
                InternalExtendActionUtil.DeleteActionId);
    }

    @Override
    protected ArrayList<AfterDeleteAction> getAfterDeleteActions(DeleteContext deleteContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getAfterDeleteActions();
        return getActions(AfterDeleteAction.class, actions, getParamTypes(), getParamValues(deleteContext), null, null);
    }

    @Override
    protected ArrayList<BeforeMultiDeleteAction> getBeforeMultiDeleteActions(DeleteContext delContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getBeforeMultiDeleteActions();
        return getActions(BeforeMultiDeleteAction.class, actions, getParamTypes(), getParamValues(delContext), null, null);
    }

    @Override
    protected ArrayList<AbstractMultiDeleteAction> getMultiDeleteActions(DeleteContext delContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getMultiDeleteActions();
        return getActions(
                AbstractMultiDeleteAction.class,
                actions,
                getParamTypes(),
                getParamValues(delContext),
                new DefaultMultiDeleteAction(delContext),
                InternalExtendActionUtil.MultiDeleteActionId);
    }

    @Override
    protected ArrayList<AfterMultiDeleteAction> getAfterMultiDeleteActions(DeleteContext delContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getAfterMultiDeleteActions();
        return getActions(AfterMultiDeleteAction.class, actions, getParamTypes(), getParamValues(delContext), null, null);
    }

    private ArrayList<Class> getParamTypes() {
        ArrayList<Class> paramTypes = new ArrayList<>();
        paramTypes.add(DeleteContext.class);
        return paramTypes;
    }

    private ArrayList<Object> getParamValues(DeleteContext deleteContext) {
        ArrayList<Object> values = new ArrayList<>();
        values.add(deleteContext);
        return values;
    }
}
